package com.yuankun.chat.controller;

import com.yuankun.chat.entity.vo.MessageVo;
import com.yuankun.chat.server.ChatHistoryReposity;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @Author：yuankun
 * @Package：com.yuankun.chat.controller
 * @Project：chat
 * @name：ChatHistoryController
 * @Date：2025/8/18 19:52
 * @Filename：ChatHistoryController
 */
@RestController
@RequestMapping("/ai/history")
@RequiredArgsConstructor
public class ChatHistoryController {
    private final ChatHistoryReposity chatHistoryReposity;
    private final ChatMemory chatMemory;

    @GetMapping("/{type}")
    public List<String> getChatHistoryReposity(@PathVariable String type) {
        List<String> chatIds = chatHistoryReposity.getChatIds(type);
        return chatIds;
    }
    @GetMapping("/{type}/{chatId}")
    public List<MessageVo> getChatHistoryReposity(@PathVariable String type, @PathVariable String chatId) {
        List<Message> messages = chatMemory.get(chatId);
        if(messages==null) return Collections.emptyList();
        return messages.stream().map(MessageVo::new).toList();
    }
}
